package com.google.code.shardbatis.converter;

import java.util.List;

import net.sf.jsqlparser.statement.Statement;
import net.sf.jsqlparser.statement.select.Select;

public class SelectSqlConverter extends AbstractSqlConverter {
	
	@Override
	public List<SqlConverterOrderBy> getOrderBys(Statement statement) {
		
		if (!(statement instanceof Select)) {
			throw new IllegalArgumentException("The argument statement must is instance of Select.");
		}

		SelectSqlConverterModifier modifier = new SelectSqlConverterModifier();
		((Select) statement).getSelectBody().accept(modifier);
		return modifier.getOrderBys();
	}
	
	@Override
	public String getOriginalTableName(Statement statement) {
		
		if (!(statement instanceof Select)) {
			throw new IllegalArgumentException("The argument statement must is instance of Select.");
		}

		SelectSqlConverterModifier modifier = new SelectSqlConverterModifier();
		((Select) statement).getSelectBody().accept(modifier);
		return modifier.getOriginalTable();
	}

	@Override
	protected Statement doConvert(Statement statement, String tableName) {
		if (!(statement instanceof Select)) {
			throw new IllegalArgumentException("The argument statement must is instance of Select.");
		}

		SelectSqlConverterModifier modifier = new SelectSqlConverterModifier(tableName);
		((Select) statement).getSelectBody().accept(modifier);
		return statement;
	}
	
}